package cn.net.xyan.blossom.security.entity

import cn.net.xyan.blossom.core.support.TableSequenceGenerator
import cn.net.xyan.blossom.kotlin.NoArg
import org.hibernate.annotations.GenericGenerator
import javax.persistence.*
import javax.persistence.JoinColumn
import javax.persistence.JoinTable
import javax.persistence.ManyToMany
import javax.persistence.GeneratedValue
import java.io.Serializable

@Entity
@NoArg
@Table(name = "sys_role")
data class SecurityRole(
        // 角色标识程序中判断使用,如"admin",这个是唯一的:
        @Column(unique = true)
        var role: String) : Serializable {

    // 编号
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SecurityRoleSequence")
    @GenericGenerator(name = "SecurityRoleSequence", strategy = TableSequenceGenerator.STRATEGY)
    private val id: String? = null

    // 角色描述,UI界面显示使用
    var description: String? = null

    // 是否可用,如果不可用将不会添加给用户
    var available = false

    //角色 -- 权限关系：多对多关系;
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "sys_role_permission_join", joinColumns = arrayOf(JoinColumn(name = "roleId")), inverseJoinColumns = arrayOf(JoinColumn(name = "permissionId")))
    var permissions: MutableSet<SecurityPermission> = HashSet()

    // 用户 - 角色关系定义;
    @ManyToMany(mappedBy = "roles")
    var userInfos: MutableSet<SecurityUserInfo> = HashSet()

}


@Entity
@Table(name = "sys_permission")
@NoArg
class SecurityPermission(
        //名称.
        @Column(unique = true)
        var name: String?) : Serializable {

    //主键.
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SecurityPermissionSequence")
    @GenericGenerator(name = "SecurityPermissionSequence", strategy = TableSequenceGenerator.STRATEGY)
    private val id: String? = null

//    //资源类型，[menu|button]
//    @Column(columnDefinition = "enum('menu','button')")
//    var resourceType: String? = null

//    //资源路径.
//    var url: String? = null

    //权限字符串,menu例子：role:*，button例子：role:create,role:update,role:delete,role:view
    var permission: String? = null

    @ManyToOne
    @JoinColumn(name = "parent")
    var parent: SecurityPermission? = null

    var available = false

    @ManyToMany(mappedBy = "permissions")
    val roles: MutableSet<SecurityRole> = HashSet()


}

